//
//  TotalSearchViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/27.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "TotalSearchViewModel.h"

#import "CommonNewsItemViewModel.h"

#import "ArticlesListData.h"

@interface TotalSearchViewModel()

@property (nonatomic, readwrite, strong) RACCommand* searchDataCmd;

@property (nonatomic, readwrite, strong) RACCommand* selectCmd;

@end

@implementation TotalSearchViewModel

- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    self.cateID = @"";
    
    self.cellHeight = 94.0f;
    
    self.lastPage = 100;
    self.shouldMultiSections = NO;
    /// viewDidLoad后加载数据
    self.shouldRequestRemoteDataOnViewDidLoad = NO;
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = NO;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = YES;
    
    /// 选中
    //    self.selectCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(ArticleItemViewModel * itemViewModel) {
    //        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    //            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), ^{
    //                [subscriber sendNext:itemViewModel];
    //                [subscriber sendCompleted];
    //            });
    //            return nil;
    //        }];
    //    }];
    /// 允许并行执行
    self.selectCmd.allowsConcurrentExecution = NO;
    
    [RACObserve(self, search) subscribeNext:^(id x)
     {
         @strongify(self);
         
         [self.searchDataCmd execute:nil];
     }];
    
    self.searchDataCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
                          {
                              @strongify(self);
                              
                              [self.requestRemoteDataCommand execute:@0];
                              
                              return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                                      {
                                          [subscriber sendNext:nil];
                                          [subscriber sendCompleted];
                                          
                                          return nil;
                                      }];
                          }];
}

- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page
{
    @weakify(self)
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self)
        
        NSDictionary* postData = interface_param_articlesearch(self.search, self.cateID, @"", @"", @(page+1), @(List_PageSize));
        
        @weakify(self);
        return [[AFNetWorkUtils racPOSTWthURL:url_interface_articlesearch params:postData] subscribeNext:^(id x)
                {
                    @strongify(self)
                    
                    /// 转化数据
                    @weakify(self);
                    
                    ArticlesListData* alistData = [ArticlesListData modelWithJSON:x];
                    
                    NSArray *viewModels = [alistData.datas.rac_sequence map:^CommonNewsItemViewModel *(ArticleListData* data)
                                           {
                                               @strongify(self);
                                               
                                               CommonNewsItemViewModel *itemViewModel = [[CommonNewsItemViewModel alloc] initWithData:data cellHeight:self.cellHeight];
                                               
                                               return itemViewModel;
                                           }].array;
                    
                    if (self.shouldPullUpToLoadMore)
                    {
                        if (0 == page)
                        {
                            self.dataSource = viewModels;
                        }
                        else
                        {
                            if (self.dataSource == nil)
                            {
                                self.dataSource = viewModels;
                            }
                            else
                            {
                                self.dataSource = [self.dataSource arrayByAddingObjectsFromArray:viewModels];
                            }
                        }
                    }
                    else
                    {
                        self.dataSource = viewModels;
                    }
                    
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                }   error:^(NSError *error) {
                    //错误处理
                    
                    [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:error.userInfo[customErrorInfoKey]}]];
                    [subscriber sendCompleted];
                }
                ];
        
        return nil;
    }];
}

@end
